var api = require('./../../config/api.js');
var app = getApp();
var util = require('./../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    orderData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },

  getData: function(){
    var that = this;
    var url = api.ToDoOrderList + '/' + app.globalData.userInfo.token;
    util.myRequest(url, false, function (data) {
      var infoList = data.data;
      for (var i = 0; i < infoList.length; i++){
        if (infoList[i].aoType == 'shop') {
          infoList[i].aoTakeInfo = JSON.parse(infoList[i].aoTakeInfo);
        }
      }
      that.setData({ orderData: infoList });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.globalData.shareInfo;
  },

  /**
   * 滑动切换
   */
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  /**
   * 点击切换
   */
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  // 到详情页
  goDetail: function (e) {
    wx.navigateTo({
      url: './../detail/detail?aoId=' + e.target.id
    })
  },

  // 取消订单
  channel: function (e) {
    wx.showModal({
      title: '你确定要取消订单吗',
      content: '',
      cancelText: '不',
      confirmText: '好',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 接单
  acceptOrder: function(e){
    var aoId = e.target.id;
    this.changeStatus(aoId, 1);
  },

  // 已取货
  hasTake: function (e) {
    var aoId = e.target.id;
    this.changeStatus(aoId, 2);
  },

  // 已送达
  hasServe: function (e) {
    var aoId = e.target.id;
    this.changeStatus(aoId, 3);
  },

  // 更改订单状态
  changeStatus: function (aoId, status){
    var that = this;
    var postData = {};
    postData.token = app.globalData.userInfo.token;
    postData.aoId = aoId;
    postData.status = status;
    console.log(postData);
    util.myRequest(api.knightChangeOrderStatus, postData, function(data){
      that.getData();
    });
  }
})